//
//  HealthData.m
//  SHHCPhone
//
//  Created by SL on 14-8-11.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "HealthData.h"

@implementation HealthData

//保存健康动态数据
+(BOOL)saveHealthData:(NSArray*)dataArray
{
    __block BOOL s = NO;
    [databasequeue inTransaction:^(FMDatabase *db, BOOL *rollback) {

        BOOL rs = [db executeUpdate:@"delete from healthdynamic"];
        if (rs == YES) {
            for (NSDictionary *dic in dataArray) {
                s = [db executeUpdate:@"REPLACE INTO healthdynamic (id,msg) VALUES (?,?)",[dic valueForKey:@"id"],[dic JSONString]];
            }
        }
    }];
    return s;
}

//获取健康动态数据
+(NSArray*)getHealthData:(NSString*)ID
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    
    [databasequeue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        NSString *sql = nil;
        if(ID){
            sql = [NSString stringWithFormat:@"SELECT * FROM healthdynamic where id < %@ order by id desc limit %d",ID,IOS_GET_NUMBER];
        }else{
            sql = [NSString stringWithFormat:@"SELECT * FROM healthdynamic order by id desc limit %d",IOS_GET_NUMBER];
        }
        
        FMResultSet *rs = [db executeQuery:sql];
        if(rs != nil)
        {
            while([rs next])
            {
                [array addObject:[rs stringForColumn:@"msg"]];
            }
            [rs close];
        }
    }];
    
    return array.count?array:nil;
}

@end
